package com.micmac.customer.view.mediator
{
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.ProductModel;
	import com.micmac.customer.view.ProductView;
	import com.micmac.customer.vo.ProductFeaturesVO;
	import com.micmac.customer.vo.ProductTitleVO;
	
	import flash.events.Event;
	
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;

	public class ProductViewMediator extends ModuleMediator
	{
		[Inject]
		public var productView:ProductView;
		[Inject]
		public var service:ICustomerService;
		[Inject]
		public var _dataModel:ProductModel;
		
		private var _productTitleIndex:int = 0;
		private var _productFeatureIndex:int = 0;
		
		override public function onRegister():void
		{
			addViewListener(ProductView.PRODUCT_TITLE_SAVE,onTitleSave);
			addViewListener(ProductView.PRODUCT_FEATURE_SAVE,onTitleFeatureSave);
			
			addViewListener(ProductView.PRODUCT_TITLE_PREVIOUS,onProductTitlePrevious);
			addViewListener(ProductView.PRODUCT_FEATURE_PREVIOUS,onProductFeaturePrevious);
			
			addViewListener(ProductView.PRODUCT_TITLE_NEXT,onProductTitleNext);
			addViewListener(ProductView.PRODUCT_FEATURE_NEXT,onProductFeatureNext);
			
			addViewListener(ProductView.STACK_CHANGED,onStackChanged);
			
			eventMap.mapListener(eventDispatcher,CustomerEvent.PRODUCT_TITLE_SAVED,onProductTitleSaved);
			eventMap.mapListener(eventDispatcher,CustomerEvent.PRODUCT_FEATURE_SAVED,onProductFeatureSaved);
		}
		
		private function onTitleSave(event:Event):void
		{
			var productTitleVo:ProductTitleVO = new ProductTitleVO();
			productTitleVo.name = productView.productTitlenameText.text;
			productTitleVo.description = productView.productTitleDescText.text;
			productTitleVo.releasePack = productView.productTitleRelesePackCombo.textInput.text;
			productTitleVo.version = productView.productTitleVersionText.text;
			productTitleVo.versionDesc = productView.productTitleVerionDescTextArea.text;
			productTitleVo.status = productView.productTitleStatus.text;
			productTitleVo.state = productView.productTitleState.text;
			service.saveProductTitle(productTitleVo);
		}
		
		private function onTitleFeatureSave(event:Event):void
		{
			var productFeatureVo:ProductFeaturesVO = new ProductFeaturesVO();
			productFeatureVo.name = productView.productFeatureName.text;
			productFeatureVo.description = productView.productFeautureDesc.text;
			productFeatureVo.benefit = productView.productFeaureBenefit.text;
			productFeatureVo.status = productView.proDuctFeatureStatusCombo.textInput.text;
			service.saveProductFeature(productFeatureVo);
		}
		
		private function onProductTitleSaved(event:CustomerEvent=null):void
		{
			if(_dataModel.productTitleList.length != 1 && _dataModel.productTitleList.length != 0)
			{
				productView.prevBtn.enabled = true;
			}else
			{
				productView.prevBtn.enabled = false;
			}
			
			productView.addProduct.enabled = true;
			productView.saveBtn.enabled = false;
			productView.nextBtn.enabled = false;
			_productTitleIndex = _dataModel.productTitleList.length - 1;
			productView.addProduct.label = '+title';
		}
		
		private function onProductFeatureSaved(event:CustomerEvent=null):void
		{
			if(_dataModel.productFeatureList.length != 1 && _dataModel.productFeatureList.length != 0)
			{
				productView.prevBtn.enabled = true;
			}else
			{
				productView.prevBtn.enabled = false;
			}
			
			productView.nextBtn.enabled = false;
			productView.addProduct.enabled = true;
			productView.saveBtn.enabled = false;
			_productFeatureIndex = _dataModel.productFeatureList.length - 1;
			productView.addProduct.label = '+product';
		}
		
		private function onProductTitlePrevious(event:Event):void
		{
			_productTitleIndex--;
			productView.nextBtn.enabled = true;
			productView.saveBtn.enabled = false;
			
			productView.productTitlenameText.text = _dataModel.productTitleList[_productTitleIndex].name;
			productView.productTitleDescText.text = _dataModel.productTitleList[_productTitleIndex].description;
			productView.productTitleRelesePackCombo.textInput.text = _dataModel.productTitleList[_productTitleIndex].releasePack;
			productView.productTitleVersionText.text = _dataModel.productTitleList[_productTitleIndex].version;
			productView.productTitleVerionDescTextArea.text = _dataModel.productTitleList[_productTitleIndex].versionDesc;
			productView.productTitleStatus.text = _dataModel.productTitleList[_productTitleIndex].status;
			productView.productTitleState.text = _dataModel.productTitleList[_productTitleIndex].state;
			
			if(_productTitleIndex == 0)
			{
				productView.prevBtn.enabled = false;
			}
		}
		
		private function onProductFeaturePrevious(event:Event):void
		{
			_productFeatureIndex--;
			productView.nextBtn.enabled = true;
			productView.saveBtn.enabled = false;
			
			productView.productFeatureName.text = _dataModel.productFeatureList[_productFeatureIndex].name;
			productView.productFeautureDesc.text = _dataModel.productFeatureList[_productFeatureIndex].description;
			productView.productFeaureBenefit.text = _dataModel.productFeatureList[_productFeatureIndex].benefit;
			productView.proDuctFeatureStatusCombo.textInput.text = _dataModel.productFeatureList[_productFeatureIndex].status;
			
			if(_productFeatureIndex == 0)
			{
				productView.prevBtn.enabled = false;
			}
		}
		
		private function onProductTitleNext(event:Event):void
		{
			_productTitleIndex++;
			productView.saveBtn.enabled = false;
			productView.prevBtn.enabled = true;
			
			productView.productTitlenameText.text = _dataModel.productTitleList[_productTitleIndex].name;
			productView.productTitleDescText.text = _dataModel.productTitleList[_productTitleIndex].description;
			productView.productTitleRelesePackCombo.textInput.text = _dataModel.productTitleList[_productTitleIndex].releasePack;
			productView.productTitleVersionText.text = _dataModel.productTitleList[_productTitleIndex].version;
			productView.productTitleVerionDescTextArea.text = _dataModel.productTitleList[_productTitleIndex].versionDesc;
			productView.productTitleStatus.text = _dataModel.productTitleList[_productTitleIndex].status;
			productView.productTitleState.text = _dataModel.productTitleList[_productTitleIndex].state;
			if(_productTitleIndex == _dataModel.productTitleList.length - 1)
			{
				productView.nextBtn.enabled = false;
			}
		}
		
		private function onProductFeatureNext(event:Event):void
		{
			_productFeatureIndex++;
			productView.saveBtn.enabled = false;
			productView.prevBtn.enabled = true;
			
			productView.productFeatureName.text = _dataModel.productFeatureList[_productFeatureIndex].name;
			productView.productFeautureDesc.text = _dataModel.productFeatureList[_productFeatureIndex].description;
			productView.productFeaureBenefit.text = _dataModel.productFeatureList[_productFeatureIndex].benefit;
			productView.proDuctFeatureStatusCombo.textInput.text = _dataModel.productFeatureList[_productFeatureIndex].status;
			if(_productFeatureIndex == _dataModel.productFeatureList.length - 1)
			{
				productView.nextBtn.enabled = false;
			}
		}
		
		private function onStackChanged(event:Event):void
		{
			switch(productView.vsProduct.selectedIndex)
			{
				case 0:
					onProductTitleSaved();
					productView.saveBtn.enabled = true;
					productView.addProduct.enabled = false;
					break;
				case 1:
					onProductFeatureSaved();
					productView.saveBtn.enabled = true;
					productView.addProduct.enabled = false;
					break;
			}
		}
	}
}